package o2o.service.sys.impl;

import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.agent.PostmanAgentRelationMemory;
import o2o.cache.agent.ShopAgentRelationMemory;
import o2o.cache.postman.CityPostmanMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.dao.PO.common.UserType;
import o2o.postman.model.CityPostman;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.service.sys.SystemMessagePushService;
import o2o.web.actions.notice.Notice;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2020/12/7 9:19
 * @Email: yangtongkuan@qq.com
 * @To change this template use File | Settings | File Templates.
 * @desc
 */
@Service
public class SystemMessagePushServiceImpl implements SystemMessagePushService {

    @Override
    public void pushSystemMessage(UserInfo loginUser, Integer pushUserType, String city, String county, Long agentId, String title, String content) {
        Set<String> usernameSet = listOfUsernameByLoginUserAndCondition(loginUser, pushUserType, city, county, agentId);
        if (usernameSet.isEmpty()) {
            return;
        }
        Notice.publishSystemNotice(usernameSet, pushUserType, title, content);
    }


    /**
     * 获取不通类型的用户账号信息
     *
     * @param loginUser
     * @param userType
     * @param city
     * @param county
     * @param agentId
     * @return
     */
    private Set<String> listOfUsernameByLoginUserAndCondition(UserInfo loginUser, Integer userType, String city, String county, Long agentId) {
        if (userType == null || userType == UserType.USER) {
            List<UserInfo> userInfoList = UserInfoMemory.getInstance().listV2();
            if (ValidateUtil.isAbsList(userInfoList)) {
                return userInfoList.stream().filter(item -> ValidateUtil.isNotNull(item.getUsername())).map(item -> item.getUsername()).collect(Collectors.toSet());
            }
            return Collections.emptySet();
        }
        if (ValidateUtil.isID(agentId)) {
            if (userType - UserType.POSTMAN == 0) {
                List<String> list = PostmanAgentRelationMemory.getInstance().listOfAgentId(agentId);
                Set<String> usernameSet = new HashSet<>();
                list = list == null ? Collections.emptyList() : list;
                list.forEach(item -> {
                    CityPostman postman = CityPostmanMemory.getInstance().getPostman(item);
                    if (postman != null) {
                        usernameSet.add(postman.getUsername());
                    }
                });
                return usernameSet;
            }
            if (userType - UserType.SHOP_OWNER == 0) {
                Set<String> usernameSet = new HashSet<>();
                List<Long> shopIdList = ShopAgentRelationMemory.getInstance().listOfAgentId(agentId);
                if (ValidateUtil.isAbsList(shopIdList)) {
                    shopIdList.forEach(item -> {
                        ShopInfo shop = ShopMemory.getInstance().getShopById(item);
                        if (shop == null) {
                            usernameSet.add(shop.getOwnName());
                        }
                    });
                }
                return usernameSet;
            }
            return Collections.emptySet();
        }

        // 查询
        if (userType - UserType.POSTMAN == 0) {
            Set<String> usernameSet = new HashSet<>();
            List<CityPostman> postmanList = CityPostmanMemory.getInstance().listOfCity(city, null, null, null, null, null, null);
            if (ValidateUtil.isAbsList(postmanList)) {
                for (CityPostman postman : postmanList) {
                    if (ValidateUtil.isNotNull(county) && (!county.equals(postman.getCounty()))) {
                        continue;
                    }
                    usernameSet.add(postman.getUsername());
                }
            }
            return usernameSet;
        }
        if (userType - UserType.SHOP_OWNER == 0) {
            Set<String> usernameSet = new HashSet<>();
            List<ShopInfo> shopInfoList = ShopMemory.getInstance().getCityShopList(city, county, null, null);
            if (ValidateUtil.isAbsList(shopInfoList)) {
                for (ShopInfo info : shopInfoList) {
                    usernameSet.add(info.getOwnUsername());
                }
            }
            return usernameSet;
        }
        return Collections.emptySet();
    }

}
